import 'dart:math';

import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:cbec_app/utils/Tools.dart';

import '../../entity/home/content/com_title06_entity.dart';

class Decoration06TitleText extends StatelessWidget {
  final ComTitle06Entity model;

  const Decoration06TitleText({super.key, required this.model});

  @override
  Widget build(BuildContext context) {
    logger.i("Decoration06TitleText === ${model.toJson()}");

    return Container(
      color: Tools.fromHex(model.bgColor ?? ''),
      width: double.infinity, // 确保容器宽度填充满屏幕
      padding: EdgeInsets.only(left: model.pageMargin!.toDouble(), right: model.pageMargin!.toDouble(), top: model.pageMarginTop!.toDouble(), bottom: model.pageMarginBottom!.toDouble()),
      child: Stack(
        children: [
          BottomImageStyleWidget(imageUrl: model.bgImage!.attachmentUrl, model: model),
          TopContentWidget(model: model),
        ],
      ),
    );
  }
}

class BottomImageStyleWidget extends StatelessWidget {
  final String? imageUrl;
  final ComTitle06Entity model;

  const BottomImageStyleWidget({super.key, this.imageUrl, required this.model});

  @override
  Widget build(BuildContext context) {
    return Positioned.fill(
      child: ClipRRect(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(model.showTopRadius! ? model.radiusNum!.toDouble() : 0), // 左上角
          topRight: Radius.circular(model.showTopRadius! ? model.radiusNum!.toDouble() : 0), // 右上角
          bottomLeft: Radius.circular(model.showBottomRadius! ? model.radiusNum!.toDouble() : 0), // 左下角
          bottomRight: Radius.circular(model.showBottomRadius! ? model.radiusNum!.toDouble() : 0), // 右下角
        ),
        child: imageUrl != null
            ? Image.network(
                imageUrl!,
                fit: BoxFit.cover,
              )
            : Container(
                color: Tools.fromHex(model.bgColor!),
              ),
      ),
    );
  }
}

class TopContentWidget extends StatefulWidget {
  final ComTitle06Entity model;

  const TopContentWidget({super.key, required this.model});

  @override
  State<TopContentWidget> createState() => _TopContentWidgetState();
}

class _TopContentWidgetState extends State<TopContentWidget> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(widget.model.pageMarginIner!.toDouble()),
      child: Align(
        alignment: getAlignment(widget.model.textAlignType!), // 根据文本对齐类型设置整体对齐
        child: Column(
          mainAxisSize: MainAxisSize.min, // 只占用所需空间
          children: [
            Padding(
              padding: const EdgeInsets.all(5),
              child: Text(
                widget.model.mainText!,
                style: TextStyle(
                  fontSize: double.parse(widget.model.titleSize!),
                  fontWeight: getFontWeightFromString(widget.model.titleWeight!),
                  color: Tools.fromHex(widget.model.titleColor!),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(5),
              child: Text(
                widget.model.subText!,
                style: TextStyle(
                  fontSize: double.parse(widget.model.descSize!),
                  fontWeight: getFontWeightFromString(widget.model.descWeight!),
                  color: Tools.fromHex(widget.model.descColor!),
                ),
              ),
            ),
            Visibility(
              visible: widget.model.showMore! && (widget.model.link != null),
              child: Padding(
                  padding: const EdgeInsets.all(5),
                  child: generateButton(widget.model, () {
                    Tools.jumpPage(widget.model.link!.toJson());
                  })),
            ),
          ],
        ),
      ),
    );
  }
}

Alignment getAlignment(String style) {
  if (style == 'left') {
    return Alignment.centerLeft;
  } else if (style == 'center') {
    return Alignment.center;
  } else {
    return Alignment.centerRight;
  }
}

MainAxisAlignment getMainAxisAlignment(String style) {
  logger.i('style ==== $style');
  if (style == 'left') {
    return MainAxisAlignment.start;
  } else if (style == 'center') {
    return MainAxisAlignment.center;
  } else {
    return MainAxisAlignment.end;
  }
}

CrossAxisAlignment getCrossAxisAlignment(String style) {
  logger.i('style ==== $style');
  if (style == 'left') {
    return CrossAxisAlignment.start;
  } else if (style == 'center') {
    return CrossAxisAlignment.center;
  } else {
    return CrossAxisAlignment.end;
  }
}

Widget generateButton(ComTitle06Entity model, VoidCallback onPressed) {
  return OutlinedButton(
    onPressed: onPressed,
    style: OutlinedButton.styleFrom(
      side: BorderSide(color: int.parse(model.moreType!) == 0 ? Tools.fromHex(model.descColor!) : Colors.transparent),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(model.radiusNum!.toDouble()),
      ),
      padding: int.parse(model.moreType!) == 0 ? const EdgeInsets.symmetric(vertical: 0, horizontal: 5) : const EdgeInsets.all(0), // 设置内边距，根据类型调整
    ),
    child: int.parse(model.moreType!) == 0
        ? Text(
            model.moreText!,
            style: TextStyle(
              fontSize: double.parse(model.descSize!),
              fontWeight: getFontWeightFromString(model.descWeight!),
              color: Tools.fromHex(model.descColor!),
              height: 0.8, // 设置行高为1，尽量贴合边框
            ),
          )
        : SizedBox(
            width: 20, // 限制图片宽度
            height: 20, // 限制图片高度
            child: Image.network(
              model.moreImage!.attachmentUrl!,
              fit: BoxFit.cover, // 适应模式
            ),
          ),
  );
}
